package jp.co.lkg.springsmp.controllers;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import jp.co.lkg.springsmp.common.ControllerConstants;
import jp.co.lkg.springsmp.common.GetMessage;
import jp.co.lkg.springsmp.common.Md5Utils;
import jp.co.lkg.springsmp.common.Page;
import jp.co.lkg.springsmp.common.TextUtils;
import jp.co.lkg.springsmp.common.db.dto.BirthplaceDto;
import jp.co.lkg.springsmp.common.db.dto.FlowerinfoDto;
import jp.co.lkg.springsmp.common.db.dto.FlowertypeinfoDto;
import jp.co.lkg.springsmp.common.db.dto.GoodinfoDto;
import jp.co.lkg.springsmp.common.db.dto.OrderinfoDto;
import jp.co.lkg.springsmp.common.db.dto.PrivatedetailDto;
import jp.co.lkg.springsmp.common.db.dto.PrivateinfoDto;
import jp.co.lkg.springsmp.common.db.dto.UserinfoDto;
import jp.co.lkg.springsmp.controllers.form.UserinfoForm;
import jp.co.lkg.springsmp.services.BirthplaceService;
import jp.co.lkg.springsmp.services.FlowerinfoService;
import jp.co.lkg.springsmp.services.FlowertypeinfoService;
import jp.co.lkg.springsmp.services.GoodinfoService;
import jp.co.lkg.springsmp.services.OrderinfoService;
import jp.co.lkg.springsmp.services.PrivatedetailService;
import jp.co.lkg.springsmp.services.PrivateinfoService;
import jp.co.lkg.springsmp.services.UserinfoService;




@RequestMapping("/user")
@Controller
public class UserinfoController {

	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private UserinfoService userinfoService;

	@Autowired
	private BirthplaceService birthplaceService;
	
	@Autowired
	private FlowertypeinfoService flowertypeinfoService;
	
	@Autowired
	private GoodinfoService goodinfoService;
	
	@Autowired
	private OrderinfoService orderinfoService;
	
	@Autowired
	private FlowerinfoService flowerinfoService;
	
	@Autowired
	private PrivateinfoService privateinfoService;
	
	@Autowired
	private PrivatedetailService privatedetailService;
	/**********************************************************************************************************************
	 * 程序启动后的首页跳转
	 */
	@RequestMapping("/test")
	public ModelAndView show(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		ModelAndView modelAndView = new ModelAndView("login");
		return modelAndView;
	}

	/**********************************************************************************************************************
	 * 实现登录的功能
	 */
	@RequestMapping("/login")
	public ModelAndView login(Integer currPage,
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		HttpSession se = request.getSession();
		// 判断验证码是否正确
		String codeval = (String) se.getAttribute("verifyCodeValue");
		String codevall = codeval.toLowerCase();
		String code1 = userinfoForm.getSearchDto().getCode();
		String code11 = code1.toLowerCase();
		String username = userinfoForm.getSearchDto().getUsername();
		String userpwd = Md5Utils.md5(userinfoForm.getSearchDto().getUserpwd());
		String code = userinfoForm.getSearchDto().getCode();
		UserinfoDto dto = new UserinfoDto();
		dto.setUsername(username);
		dto.setUserpwd(userpwd);
		dto.setCode(code);
		
		String msg = validate(dto);
		if (TextUtils.isEmpty(msg)) {// 错误信息为空
			if (!code11.equals(codevall)) {
				msg = "验证码错误";
				request.setAttribute("msg", msg);
				ModelAndView modelAndView = new ModelAndView("login");
				return modelAndView;
			} else {
				List<UserinfoDto> userinfoList = userinfoService.select(dto);
				
				if (userinfoList != null && userinfoList.size() > 0) {
					request.getSession().setAttribute("sessionUser", userinfoList.get(0));
					request.getSession().setMaxInactiveInterval(60 * 60 * 60);
					// *************这边是根据用户名来查询用户的权限来跳转不同的界面 ********************//
					String userstatus = userinfoService.findStatus(username);
					if (userstatus.equals(ControllerConstants.AttributeKeys.KEY_ADMIN)) {
						// 管理员登录
						ModelAndView modelAndView = new ModelAndView("admin");
						return modelAndView;
					} else {
						// 用户登录
						// 将登陆者的信息存在session中
						userinfoForm.setUserinfoList(userinfoList);
						
						
						
						//将一些需要在界面中显示的值传
						GoodinfoDto goodinfoDto=new GoodinfoDto();
						
						Page pagelist = goodinfoService.selectPage(currPage, goodinfoDto);
						FlowertypeinfoDto dto2=new FlowertypeinfoDto();
						List<FlowertypeinfoDto> flowertypelist=flowertypeinfoService.select(dto2);
						String goodType=request.getParameter("goodType");
						request.setAttribute("goodType", goodType);
						model.addAttribute("flowertypelist",flowertypelist);
						model.addAttribute("pagelist", pagelist);
						
						ModelAndView modelAndView = new ModelAndView("main");
						return modelAndView;
					}
					// 用户名和密码不正确时
				} else {
					msg = "用户名或密码错误";
					request.setAttribute("msg", msg);
					ModelAndView modelAndView = new ModelAndView("login");
					return modelAndView;
				}
			}
		} else {
			request.setAttribute("msg", msg);
			ModelAndView modelAndView = new ModelAndView("login");
			return modelAndView;
		}
	}
	/**********************************************************************************************************************
	 * 跳转到手机登录界面
	 */
	@RequestMapping(value = "/getCode", method = RequestMethod.POST)
	@ResponseBody
	public String getCode(HttpServletRequest request) {
			String phone=request.getParameter("phone");
			String result=GetMessage.getResult(phone);
		
			return result;
			
	}
	/**********************************************************************************************************************
	 * 跳转到手机登录界面
	 */
	
	@RequestMapping("/phonelogin")
	public ModelAndView phonelogin(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		
		ModelAndView modelAndView = new ModelAndView("phonelogin");
		return modelAndView;
		
	}
	
	@RequestMapping("/plogin")
	public ModelAndView plogin(Integer currPage,
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		String phone=userinfoForm.getSearchDto().getPhone();
		String code=userinfoForm.getSearchDto().getCode();
		String pcode=GetMessage.randNum;
		UserinfoDto dto=new UserinfoDto();
		dto.setPhone(phone);
		List<UserinfoDto> userinfoList=userinfoService.select(dto);
		String msg=null;
		
		if (code.equals(pcode)) {
			if (userinfoList != null && userinfoList.size() > 0) {
				request.getSession().setAttribute("sessionUser", userinfoList.get(0));
				request.getSession().setMaxInactiveInterval(60 * 60 * 60);
				String username=userinfoList.get(0).getUsername();
				String userstatus = userinfoService.findStatus(username);
				
				
				if (userstatus.equals(ControllerConstants.AttributeKeys.KEY_ADMIN)) {
					// 管理员登录
					ModelAndView modelAndView = new ModelAndView("admin");
					return modelAndView;
				} else {
					// 用户登录
					// 将登陆者的信息存在session中
					userinfoForm.setUserinfoList(userinfoList);
					//将一些需要在界面中显示的值传
					GoodinfoDto goodinfoDto=new GoodinfoDto();
					
					Page pagelist = goodinfoService.selectPage(currPage, goodinfoDto);
					FlowertypeinfoDto dto2=new FlowertypeinfoDto();
					List<FlowertypeinfoDto> flowertypelist=flowertypeinfoService.select(dto2);
					String goodType=request.getParameter("goodType");
					request.setAttribute("goodType", goodType);
					model.addAttribute("flowertypelist",flowertypelist);
					model.addAttribute("pagelist", pagelist);
					
					ModelAndView modelAndView = new ModelAndView("main");
					return modelAndView;
				}
				
			}else {
				msg="没有该账号";
				request.setAttribute("msg", msg);
				ModelAndView modelAndView = new ModelAndView("phonelogin");
				return modelAndView;
			}
		}else 
		{
			msg="验证码错误";
			request.setAttribute("msg", msg);
			ModelAndView modelAndView = new ModelAndView("phonelogin");
			return modelAndView;
		}
		
	
		
	}
	/**********************************************************************************************************************
	 * 跳转到注册界面
	 */
	@RequestMapping("/first")
	public ModelAndView first(Integer currPage,
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {

		GoodinfoDto goodinfoDto=new GoodinfoDto();
		
		Page pagelist = goodinfoService.selectPage(currPage, goodinfoDto);
		FlowertypeinfoDto dto2=new FlowertypeinfoDto();
		List<FlowertypeinfoDto> flowertypelist=flowertypeinfoService.select(dto2);
		String goodType=request.getParameter("goodType");
		request.setAttribute("goodType", goodType);
		model.addAttribute("flowertypelist",flowertypelist);
		model.addAttribute("pagelist", pagelist);
		
		ModelAndView modelAndView = new ModelAndView("main");
		return modelAndView;
	}
	
	/**********************************************************************************************************************
	 * 跳转到注册界面
	 */
	@RequestMapping("/toregister")
	public ModelAndView toregister(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {

		BirthplaceDto privince = new BirthplaceDto();
		privince.setGrade("0");
		List<BirthplaceDto> provinceFieldList = birthplaceService.select(privince);

		System.out.println(provinceFieldList);
		userinfoForm.setProvinceFieldList(provinceFieldList);

		model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);

		ModelAndView modelAndView = new ModelAndView("register");

		return modelAndView;
	}

	/**********************************************************************************************************************
	 * 跳转到注册界面
	 */
	@RequestMapping("/register")
	public ModelAndView register(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {

		String msg = validate1(userinfoForm.getInsertDto());

		if (TextUtils.isEmpty(msg)) {
			// 这边我是设置了用户名唯一，注册的时候先去数据库查询，输入的用户明是否已经存在
			String username = request.getParameter("insertDto.username");
			UserinfoDto userinfoDto = new UserinfoDto();
			userinfoDto.setUsername(username);
			List<UserinfoDto> userinfoList = userinfoService.select(userinfoDto);

			if (userinfoList != null && userinfoList.size() > 0) {
				msg = "用户名已经存在";
				request.setAttribute("msg", msg);
				toregister(userinfoForm, request, response, model);
				ModelAndView modelAndView = new ModelAndView("register");
				return modelAndView;
			} else {
				String pwd = request.getParameter("insertDto.userpwd");
				String userpwd = Md5Utils.md5(pwd);
				UserinfoDto dto = userinfoForm.getInsertDto();
				dto.setUserpwd(userpwd);
				userinfoService.insert(dto);
				ModelAndView modelAndView = new ModelAndView("login");
				return modelAndView;
			}
		} else {
			request.setAttribute("msg", msg);
			toregister(userinfoForm, request, response, model);
			ModelAndView modelAndView = new ModelAndView("register");
			return modelAndView;
		}

	}

	/**********************************************************************************************************************
	 * 添加订单
	 * 
	 */
	@RequestMapping("/addorder")
	public ModelAndView addorder(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			String goodId=request.getParameter("goodId");
			OrderinfoDto dto=userinfoForm.getOrderinsertDto();
			orderinfoService.insert(dto);
			String goodName=userinfoForm.getOrderinsertDto().getOrderName();
			GoodinfoDto goodinfoDto=goodinfoService.selectByName(goodName);
			
			int stock=goodinfoDto.getGoodStock();
			int sales=goodinfoDto.getGoodSales();
			int nowstock=stock-1;
			int nowsales=sales+1;
			if (nowstock>=0) {
				GoodinfoDto dto1=new GoodinfoDto();
				dto1.setGoodId(goodId);
				dto1.setGoodStock(nowstock);
				dto1.setGoodSales(nowsales);
				goodinfoService.updateByPk(dto1);
				
			}
			OrderinfoDto orderinfoDto=new OrderinfoDto();
			String username=userinfoForm.getOrderinsertDto().getOrderOwner();
			orderinfoDto.setOrderOwner(username);
			List<OrderinfoDto> orderlist=orderinfoService.select(orderinfoDto);
			userinfoForm.setOrderlist(orderlist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("order");
			return modelAndView;
			
		
	}
	
	
	/**********************************************************************************************************************
	 * 显示订单
	 * 
	 */
	@RequestMapping("/showorder")
	public ModelAndView showorder(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		
			OrderinfoDto orderinfoDto=new OrderinfoDto();
			String username=request.getParameter("username");
			orderinfoDto.setOrderOwner(username);
			List<OrderinfoDto> orderlist=orderinfoService.select(orderinfoDto);
			userinfoForm.setOrderlist(orderlist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("order");
			return modelAndView;
		
	}
	/**********************************************************************************************************************
	 * 显示订单
	 * 
	 */
	@RequestMapping("/buyorder")
	public ModelAndView buyorder(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			String orderid=request.getParameter("orderId");
			OrderinfoDto dto=new OrderinfoDto();
			dto.setOrderId(orderid);
			dto.setOrderPaystatus("支付完成");
			orderinfoService.updateByPk(dto);
			OrderinfoDto orderinfoDto=new OrderinfoDto();
			String username=request.getParameter("username");
			orderinfoDto.setOrderOwner(username);
			List<OrderinfoDto> orderlist=orderinfoService.select(orderinfoDto);
			userinfoForm.setOrderlist(orderlist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("order");
			return modelAndView;
		
	}
	
	/**********************************************************************************************************************
	 * 删除订单
	 * 
	 */
	@RequestMapping("/cancelorder")
	public ModelAndView cancelorder(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			String orderId=request.getParameter("orderId");
			String username=request.getParameter("userName");
			OrderinfoDto orderinfoDto1=new OrderinfoDto();
			orderinfoDto1.setOrderId(orderId);
			OrderinfoDto dto4=orderinfoService.selectByPk(orderinfoDto1);
			String goodName=dto4.getOrderName();
			System.out.println(goodName);
			OrderinfoDto dto=new OrderinfoDto();
			dto.setOrderId(orderId);
			orderinfoService.deleteByPk(dto);
			//取消订单时增加库存，减少销量
			GoodinfoDto goodinfoDto=goodinfoService.selectByName(goodName);
			int stock=goodinfoDto.getGoodStock();
			int sales=goodinfoDto.getGoodSales();
			System.out.println("现在的销量"+sales+"现在的库存"+stock);
			String id=goodinfoDto.getGoodId();
			int nowStock=stock+1;
			int nowSales=sales-1;
			System.out.println("删除之后的销量"+nowSales+"删除之后的库存"+nowStock);
			GoodinfoDto dto1=new GoodinfoDto();
			dto1.setGoodId(id);
			dto1.setGoodStock(nowStock);
			dto1.setGoodSales(nowSales);
			goodinfoService.updateByPk(dto1);
			
			OrderinfoDto orderinfoDto=new OrderinfoDto();
			orderinfoDto.setOrderOwner(username);
			List<OrderinfoDto> orderlist=orderinfoService.select(orderinfoDto);
			userinfoForm.setOrderlist(orderlist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("order");
			return modelAndView;
		
	}
	/**********************************************************************************************************************
	 * 商品详情
	 */

	@RequestMapping("/gooddetail")
	public String gooddetail(HttpServletRequest request, Model model) {
		String goodId=request.getParameter("goodId");
		GoodinfoDto dto=new GoodinfoDto();
		dto.setGoodId(goodId);
		List<GoodinfoDto> goodinfoDto = goodinfoService.select(dto);
		model.addAttribute("list", goodinfoDto);
		return "gooddetail";

	}
	
	/**********************************************************************************************************************
	 * 显示订单
	 * 
	 */
	@RequestMapping("/showprivateorder")
	public ModelAndView showprivateorder(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			PrivateinfoDto privateinfoDto=new PrivateinfoDto();
			String username=request.getParameter("username");
			privateinfoDto.setPrivateOwner(username);
			List<PrivateinfoDto> privatelist=privateinfoService.select(privateinfoDto);
			userinfoForm.setPrivatelist(privatelist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("privateorder");
			return modelAndView;
		
	}
	
	
	@RequestMapping("/buyprivateorder")
	public ModelAndView buyprivateorder(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		
			String privateid=request.getParameter("privateId");
			PrivateinfoDto dto=new PrivateinfoDto();
			dto.setPrivateId(privateid);
			dto.setPrivatePaystatus("支付完成");
			privateinfoService.updateByPk(dto);
			PrivateinfoDto privateinfoDto=new PrivateinfoDto();
			String username=request.getParameter("username");
			privateinfoDto.setPrivateOwner(username);
			List<PrivateinfoDto> privatelist=privateinfoService.select(privateinfoDto);
			userinfoForm.setPrivatelist(privatelist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("privateorder");
			return modelAndView;
		
	}

	
	/**********************************************************************************************************************
	 * 去往密码界面
	 * 
	 */
	@RequestMapping("/pwd")
	public ModelAndView pwd(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			ModelAndView modelAndView = new ModelAndView("pwd");
			return modelAndView;
		
	}
	/**********************************************************************************************************************
	 * 去往个人信息
	 * 
	 */
	@RequestMapping("/touser")
	public String touser(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			String username=request.getParameter("username");
			String id=userinfoService.findId(username);
			UserinfoDto dto=new UserinfoDto();
			dto.setId(id);
			dto.setUsername(username);
			List<UserinfoDto> userlist=userinfoService.select(dto);
			model.addAttribute("userlist", userlist);
			return "userinfo";
		
	}
	
	/**********************************************************************************************************************
	 * 去往个人信息
	 * 
	 */
	@RequestMapping("/updateuser")
	public ModelAndView updateuser(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			userinfoService.updateByPk(userinfoForm.getUpdateDto());
			ModelAndView modelAndView = new ModelAndView("userinfo");
			return modelAndView;
		
	}
	/**********************************************************************************************************************
	 * 去往密码界面
	 * 
	 */
	@RequestMapping("/updatePwd")
	public ModelAndView updatePwd(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			String username=userinfoForm.getSearchDto().getUsername();
			System.out.println(username);
			String userpwd=Md5Utils.md5(userinfoForm.getSearchDto().getUserpwd());
			String newPwd=Md5Utils.md5(userinfoForm.getSearchDto().getNewPwd());
			String id=userinfoService.findId(username);
			UserinfoDto dto=new UserinfoDto();
			dto.setUsername(username);
			dto.setUserpwd(userpwd);
			List<UserinfoDto> list=userinfoService.select(dto);
			System.out.println(list);
			if (list!=null && list.size()>0) {
				UserinfoDto dto2=new UserinfoDto();
				dto2.setId(id);
				dto2.setUsername(username);
				dto2.setUserpwd(newPwd);
				userinfoService.updateByPk(dto2);
			}
			
			ModelAndView modelAndView = new ModelAndView("pwd");
			return modelAndView;
		
	}
	
	
	/**********************************************************************************************************************
	 * 添加私人定制订单
	 */
	@RequestMapping("/addprivate")
	public ModelAndView addprivate(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			PrivateinfoDto dto=userinfoForm.getPrivateinsertDto();
			privateinfoService.insert(dto);
			
			//私人定制库存的管理
			List<PrivatedetailDto> privateDetailList=userinfoForm.getPrivateDetailList();
		
	
			privatedetailService.insertlist(privateDetailList);
			for (int i = 0; i < privateDetailList.size(); i++) {
				String name=privateDetailList.get(i).getName();
				int num=privateDetailList.get(i).getNum();
				String flowerid=privateDetailList.get(i).getFlowerId();
				List<FlowerinfoDto> dto1=flowerinfoService.selectByName(name);
				for (int j = 0; j < dto1.size(); j++) {
					int stock=dto1.get(j).getFlowerStock();
					int sales=dto1.get(j).getFlowerSales();
					int nowstock=stock-num;
					int nowslaes=sales+num;
					FlowerinfoDto flowerinfoDto=new FlowerinfoDto();
					flowerinfoDto.setFlowerId(flowerid);
					flowerinfoDto.setFlowerStock(nowstock);
					flowerinfoDto.setFlowerSales(nowslaes);
					flowerinfoService.updateByPk(flowerinfoDto);
				}
				
			}
			PrivateinfoDto privateinfoDto=new PrivateinfoDto();
			String username=userinfoForm.getPrivateinsertDto().getPrivateOwner();
			privateinfoDto.setPrivateOwner(username);
			List<PrivateinfoDto> privatelist=privateinfoService.select(privateinfoDto);
			userinfoForm.setPrivatelist(privatelist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("privateorder");
			return modelAndView;
		
	}
	
	/**********************************************************************************************************************
	 * 删除私人定制订单
	 * 
	 */
	@RequestMapping("/cancelprivate")
	public ModelAndView cancelprivate(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
			String privateId=request.getParameter("privateId");
			String username=request.getParameter("userName");
			PrivateinfoDto dto=new PrivateinfoDto();
			dto.setPrivateId(privateId);
			privateinfoService.deleteByPk(dto);
			//库存管理
			PrivatedetailDto dto1=new PrivatedetailDto();
			dto1.setOrderid(privateId);
			List<PrivatedetailDto>list= privatedetailService.select(dto1);
			for (int i = 0; i < list.size(); i++) {
				String name=list.get(i).getName();
				int num=list.get(i).getNum();
				String id=list.get(i).getFlowerId();
				System.out.println(name+num);
				List<FlowerinfoDto> list2=flowerinfoService.selectByName(name);
				for (int j = 0; j < list2.size(); j++) {
					int stock=list2.get(j).getFlowerStock();
					int sales=list2.get(j).getFlowerSales();
					int nowstock=stock+num;
					int nowsales=sales-num;
					FlowerinfoDto dto2=new FlowerinfoDto();
					dto2.setFlowerId(id);
					dto2.setFlowerStock(nowstock);
					dto2.setFlowerSales(nowsales);
					flowerinfoService.updateByPk(dto2);
				}
			}
			
			
			PrivateinfoDto privateinfoDto=new PrivateinfoDto();
			privateinfoDto.setPrivateOwner(username);
			List<PrivateinfoDto> privatelist=privateinfoService.select(privateinfoDto);
			userinfoForm.setPrivatelist(privatelist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("privateorder");
			return modelAndView;
		
	}

	/**********************************************************************************************************************
	 * 检验登录界面是否为空
	 */

	private String validate(UserinfoDto userinfoDto) {
		String errorMsg = null;
		if (TextUtils.isEmpty(userinfoDto.getUsername())) {
			errorMsg = "请输入用户名";
		} else if (TextUtils.isEmpty(userinfoDto.getUserpwd())) {
			errorMsg = "请输入密码";
		} else if (TextUtils.isEmpty(userinfoDto.getCode())) {
			errorMsg = "请输入验证码";
		}
		return errorMsg;
	}

	/**********************************************************************************************************************
	 * 检验注册界面是否为空
	 */

	private String validate1(UserinfoDto userinfoDto) {
		String errorMsg = null;
		if (TextUtils.isEmpty(userinfoDto.getUsername())) {
			errorMsg = "请输入用户名";
		} else if (TextUtils.isEmpty(userinfoDto.getUserpwd())) {
			errorMsg = "请输入密码";
		} else if (TextUtils.isEmpty(userinfoDto.getBirthday())) {
			errorMsg = "请输入出生日期";
		} else if (TextUtils.isEmpty(userinfoDto.getQq())) {
			errorMsg = "请输入QQ号";
		} else if (TextUtils.isEmpty(userinfoDto.getEmail())) {
			errorMsg = "请输入邮箱";
		} else if (TextUtils.isEmpty(userinfoDto.getPhone())) {
			errorMsg = "请输入电话号码";
		} else if (TextUtils.isEmpty(userinfoDto.getUsercard())) {
			errorMsg = "请输入身份证号";
		} else if (TextUtils.isEmpty(userinfoDto.getAddress())) {
			errorMsg = "请输入地址";
		}
		return errorMsg;
	}

	/**********************************************************************************************************************
	 * 随机参数一个字符
	 */
	private char randomChar() {
		Random r = new Random();
		String s = "ABCDEFGHJKLMNPRSTUVYabcdefghijkmnpqrsty23456789";
		return s.charAt(r.nextInt(s.length()));
	}

	/**********************************************************************************************************************
	 * 绘画验证码
	 */
	private String drawImg(ByteArrayOutputStream output) {
		String code = "";
		// 随机产生4个字符
		for (int i = 0; i < 4; i++) {
			code += randomChar();
		}
		int width = 97;
		int height = 37;
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
		Font font = new Font("Times New Roman", Font.PLAIN, 20);
		// 调用Graphics2D绘画验证码
		Graphics2D g = bi.createGraphics();
		g.setFont(font);
		Color color = new Color(66, 2, 82);
		g.setColor(color);
		g.setBackground(new Color(226, 226, 240));
		g.clearRect(0, 0, width, height);
		FontRenderContext context = g.getFontRenderContext();
		Rectangle2D bounds = font.getStringBounds(code, context);
		double x = (width - bounds.getWidth()) / 2;
		double y = (height - bounds.getHeight()) / 2;
		double ascent = bounds.getY();
		double baseY = y - ascent;
		g.drawString(code, (int) x, (int) baseY);
		g.dispose();
		try {
			ImageIO.write(bi, "jpg", output);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return code;
	}

	/**********************************************************************************************************************
	 * 获取验证码
	 * 
	 * @param response
	 * @param session
	 */
	@RequestMapping("getVerifyCode")
	public void generate(HttpServletResponse response, HttpSession session) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		String verifyCodeValue = drawImg(output);
		session.setAttribute("verifyCodeValue", verifyCodeValue);
		// System.out.println(verifyCodeValue);
		try {
			ServletOutputStream out = response.getOutputStream();
			output.writeTo(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**********************************************************************************************************************
	 * 省市联动通过省份来获取市
	 */
	
	@RequestMapping(value = "/getCities1", method = RequestMethod.POST)
	@ResponseBody
	public Map getCities(final String provinceCd) throws ServletException, IOException {

		final Map josnMap = new HashMap<>();
		BirthplaceDto city = new BirthplaceDto();
		List<BirthplaceDto> citylist = new ArrayList<BirthplaceDto>();
		city.setParent(provinceCd);
		citylist = birthplaceService.select(city);
		josnMap.put("fieldListExtModelList", citylist);

		return josnMap;

	}
	/**********************************************************************************************************************
	 * 根据选择的类型来跳转不同的鲜花
	 * 
	 */
	@RequestMapping(value="/getGoodInfosByType",method = RequestMethod.POST)
	@ResponseBody
    public Map getGoodInfosByType(Integer currPage,
    		final GoodinfoDto goodType)throws ServletException,IOException{	
		final Map jsonMap = new HashMap<>();
			Page goodInfosList = goodinfoService.selectPage(currPage, goodType);
			jsonMap.put("goodInfosList", goodInfosList);
		return jsonMap;
        
    }
	
	
	/**********************************************************************************************************************
	 * 私人定制模块
	 * 
	 * 
	 */
	/**********************************************************************************************************************
	 * 显示所有的花种
	 * 
	 * 
	 */
	@RequestMapping("/showflower")
	public ModelAndView showflower(
			@ModelAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM) final UserinfoForm userinfoForm,
			HttpServletRequest request, HttpServletResponse response, final Model model) {
		
			FlowerinfoDto flowerinfoDto=new FlowerinfoDto();
			List<FlowerinfoDto> flowerlist=flowerinfoService.select(flowerinfoDto);
		
			userinfoForm.setFlowerlist(flowerlist);
			model.addAttribute(ControllerConstants.AttributeKeys.KEY_USER_FORM, userinfoForm);
			ModelAndView modelAndView = new ModelAndView("privateflower");
			return modelAndView;
		
	}
	
}